/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package net.l1j.server.serverpackets;

import net.l1j.server.Opcodes;
import net.l1j.server.model.instance.L1PcInstance;

public class S_OtherCharPacks extends ServerBasePacket {
	private static final String S_OTHER_CHAR_PACKS = "[S] S_OtherCharPacks";

	private static final short STATUS_POISON = 1;
	private static final short STATUS_INVISIBLE = 2;
	private static final short STATUS_PC = 4;
	private static final short STATUS_FREEZE = 8;
	private static final short STATUS_BRAVE = 16;
	private static final short STATUS_ELFBRAVE = 32;
	private static final short STATUS_FASTMOVABLE = 64;
	private static final short STATUS_GHOST = 128;

	private byte[] _byte = null;

	public S_OtherCharPacks(L1PcInstance pc, boolean isFindInvis) {
		buildPacket(pc, isFindInvis);
	}

	public S_OtherCharPacks(L1PcInstance pc) {

		buildPacket(pc, false);
	}

	private void buildPacket(L1PcInstance pc, boolean isFindInvis) {
		short status = STATUS_PC;
		if (pc.getPoison() != null) { // 毒狀態
			if (pc.getPoison().getEffectId() == 1) {
				status |= STATUS_POISON;
			}
		}
		if (pc.isInvisble()) {
			status |= STATUS_INVISIBLE;
		}
		if (pc.isBrave()) {
			status |= STATUS_BRAVE;
		}
		if (pc.isElfBrave()) {
			// エルヴンワッフルの場合は、STATUS_BRAVEとSTATUS_ELFBRAVEを立てる。
			// STATUS_ELFBRAVEのみでは效果が無い？
			status |= STATUS_BRAVE;
			status |= STATUS_ELFBRAVE;
		}
		if (pc.isFastMovable()) {
			status |= STATUS_FASTMOVABLE;
		}

		// int addbyte = 0;
		// int addbyte1 = 1;

		writeC(Opcodes.S_OPCODE_CHARPACK);
		writeH(pc.getX());
		writeH(pc.getY());
		writeD(pc.getId());
		if (pc.isDead()) {
			writeH(pc.getTempCharGfxAtDead());
		} else {
			writeH(pc.getTempCharGfx());
		}
		if (pc.isDead()) {
			writeC(pc.getStatus());
		} else {
			writeC(pc.getCurrentWeapon());
		}
		writeC(pc.getHeading());
		// writeC(0); // makes char invis (0x01), cannot move. spells display
		writeC(pc.getChaLightSize());
		writeC(pc.getMoveSpeed());
		writeD(0x0000); // exp
		// writeC(0x00);
		writeH(pc.getLawful());
		 //  add 轉生顯示系統 0-9 BY.Henrysu Start
		 String lvmetname = "";
	       if (pc.getAccessLevel()>=100){
	    	   lvmetname = "\\f2【遊戲管理員】";}
	       else{
	    	   switch (pc.getlevelmet()){	       
	        	  case 0: lvmetname = "";break;
	        	  case 1: lvmetname = "\\f=【1轉】";break;   
	        	  case 2: lvmetname = "\\f=【2轉】";break; 
	        	  case 3: lvmetname = "\\f=【3轉】";break; 
                  case 4: lvmetname = "\\f=【4轉】";break;  
	        	  case 5: lvmetname = "\\f=【5轉】";break; 
	        	  case 6: lvmetname = "\\f=【6轉】";break; 
                  case 7: lvmetname = "\\f=【7轉】";break; 
                  case 8: lvmetname = "\\f=【8轉】";break; 
                  case 9: lvmetname = "\\f=【9轉】";break; 
} 
	       }
	          writeS(pc.getName() + lvmetname);//  add 轉生顯示系統 0-9 BY.Henrysu Start
		writeS(pc.getTitle());
		writeC(status);
		writeD(pc.getClanid());
		writeS(pc.getClanname()); // クラン名
		writeS(null); // ペッホチング？
		writeC(0); // ？
		/*
		 * if(pc.is_isInParty()) // パーティー中 { writeC(100 * pc.get_currentHp() /
		 * pc.get_maxHp()); } else { writeC(0xFF); }
		 */

		writeC(0xFF);
		writeC(0); // タルクック距離(通り)
		writeC(0); // PC = 0, Mon = Lv
		writeC(0); // ？
		writeC(0xFF);
		writeC(0xFF);
	}

	@Override
	public byte[] getContent() {
		if (_byte == null) {
			_byte = getBytes();
		}
		return _byte;
	}

	@Override
	public String getType() {
		return S_OTHER_CHAR_PACKS;
	}
}
